#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>

typedef enum Status
{
    SUCCESS,     //成功
    FAILED,      //失败
    EMPTY,       //表为空
    NOTESIST,    //表不存在
    MEMOVERFLOW, //内存溢出,空间不够
} Status;

//学生结构体
typedef struct Student
{
    char sno[11]; //学号
    char snm[30]; //姓名
    char gender;  //性别 F女 M男
    int age;      //年龄
} Student;

//学号表
typedef struct TableSNO
{
    char sno[11];          //学号项
    struct TableSNO *next; //next指针
} TableSNO, *TableSNOPtr;

//姓名表
typedef struct TableSNM
{
    char snm[30];          //姓名项
    struct TableSNM *next; //next指针
} TableSNM, *TableSNMPtr;

//年龄表
typedef struct TabelAge
{
    int age;               //年龄项
    struct TableAge *next; //next指针
} TabelAge, *TabelAgePtr;

//性别表
typedef struct TableGender
{
    char gender;              //性别 F女 M男
    struct TableGender *next; //next指针
} TableGender, *TableGenderPtr;

//表头
typedef struct TableHead
{
    TableSNOPtr snoList;       //学号的表头
    TableSNMPtr snmList;       //姓名的表头
    TabelAgePtr ageList;       //年龄的表头
    TableGenderPtr genderList; //性别的表头
} TableHead, *TableHeadPtr, **StudentTable;

Status initTable(StudentTable studentTable)
{
    //初始化表头，给表头一个头结点，头结点不存放数据
    (*studentTable) = (TableHeadPtr)malloc(sizeof(TableHead));
    if ((*studentTable) == NULL)
        return MEMOVERFLOW;
    //初始化学号字段
    (*studentTable)->snoList = (TableSNOPtr)malloc(sizeof(TableSNO));
    if ((*studentTable)->snoList == NULL)
        return MEMOVERFLOW;
    (*studentTable)->snoList->next = NULL;
    //初始化姓名字段
    (*studentTable)->snmList = (TableSNMPtr)malloc(sizeof(TableSNM));
    if ((*studentTable)->snmList == NULL)
        return MEMOVERFLOW;
    (*studentTable)->snmList->next = NULL;
    //初始化年龄字段
    (*studentTable)->ageList = (TabelAgePtr)malloc(sizeof(TabelAge));
    if ((*studentTable)->ageList == NULL)
        return MEMOVERFLOW;
    (*studentTable)->ageList->next = NULL;
    //初始化性别字段
    (*studentTable)->genderList = (TableGenderPtr)malloc(sizeof(TableGender));
    if ((*studentTable)->genderList == NULL)
        return MEMOVERFLOW;
    (*studentTable)->genderList->next = NULL;
    return SUCCESS;
}
Status inertIntoTable(StudentTable studentTable, const Student student)
{
    if ((*studentTable) == NULL)
        return NOTESIST; //表不存在，未初始化
    //分配字段节点
    TableSNOPtr snoPtr = (TableSNOPtr)malloc(sizeof(TableSNO));
    TableSNMPtr snmPtr = (TableSNMPtr)malloc(sizeof(TableSNM));
    TabelAgePtr agePtr = (TabelAgePtr)malloc(sizeof(TabelAge));
    TableGenderPtr genderPtr = (TableGenderPtr)malloc(sizeof(TableGender));
    if (snoPtr == NULL || snmPtr == NULL || agePtr == NULL || genderPtr == NULL)
    {
        return MEMOVERFLOW; //内存不足分配失败
    }
    //初始化数据域
    strcpy(snoPtr->sno, student.sno);   //字符串拷贝使用函数，不能使用=
    strcpy(snmPtr->snm, student.snm);   //字符串拷贝使用函数，不能使用=
    agePtr->age = student.age;          //基本数据类型使用=赋值即可
    genderPtr->gender = student.gender; //基本数据类型使用=赋值即可
    //指针域初始化
    snoPtr->next = NULL;
    snmPtr->next = NULL;
    agePtr->next = NULL;
    genderPtr->next = NULL;
    //将数据添加到链表中，末尾
    (*studentTable)->snoList->next = snoPtr;
    (*studentTable)->snmList->next = snmPtr;
    (*studentTable)->ageList->next = agePtr;
    (*studentTable)->genderList->next = genderPtr;
    return SUCCESS;
}
int main()
{
    TableHeadPtr studentTable = NULL;
    Status flag;
    flag = initTable(&studentTable);
    if (flag != SUCCESS)
    {
        printf("init failed!\n");
    }
    Student s = {"001", "Jack", 18, 'M'};
    flag = inertIntoTable(&studentTable, s);
    if (flag != SUCCESS)
    {
        printf("insert failed!\n");
    }
    printf("%s\n", studentTable->snmList->next->snm);
    return 0;
}